package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.lib.util.FileUtils;
import com.example.win.android_banduyujia.peopleactivity.ui.bitmap.Bimp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Win on 2015/9/10.
 */
public class AssPictureActivity extends Activity {

     private ArrayList<View>  listViews = null;
     private ViewPager        pager;
     private MyPageAdapter    adapter;
     private int   count;

     private List<Bitmap>  bmp = new ArrayList<Bitmap>();
     private List<String>  drr = new ArrayList<String>();
     private List<String>  del = new ArrayList<String>();
     private int max;

    private RelativeLayout  photo_relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_picture_item_photo);

        photo_relativeLayout=(RelativeLayout)findViewById(R.id.photo_relativeLayout);
        photo_relativeLayout.setBackgroundColor(0x70000000);

        for(int i=0; i< Bimp.bmp.size(); i++){
            bmp.add(Bimp.bmp.get(i));
        }
        for(int i=0; i< Bimp.drr.size(); i++){
            drr.add(Bimp.drr.get(i));
        }
        max = Bimp.max;

        Button photo_bt_exit=(Button)findViewById(R.id.photo_bt_exit);
        photo_bt_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Button photo_bt_del=(Button)findViewById(R.id.photo_bt_del);
        photo_bt_del.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if (listViews.size() == 1) {
                    Bimp.bmp.clear();
                    Bimp.drr.clear();
                    Bimp.max = 0;
                    FileUtils.deleteDir();
                    finish();
                } else {
                    String newStr = drr.get(count).substring(drr.get(count).lastIndexOf("/") + 1, drr.get(count).lastIndexOf("."));
                    bmp.remove(count);
                    drr.remove(count);
                    del.add(newStr);
                    max--;
                    pager.removeAllViews();
                    listViews.remove(count);
                    adapter.setListViews(listViews);
                    adapter.notifyDataSetChanged();
                }
            }
        });
        Button photo_bt_enter=(Button)findViewById(R.id.photo_bt_enter);
        photo_bt_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bimp.bmp = bmp;
                Bimp.drr = drr;
                Bimp.max = max;
                for(int i = 0; i< del.size(); i++){
                    FileUtils.delFile(del.get(i) + ".JPEG");
                }
                finish();
            }
        });

        pager = (ViewPager)findViewById(R.id.view_picture_item_pager);
        pager.setOnPageChangeListener(pageChangeListener);
        for (int i = 0; i < bmp.size(); i++) {
            initListViews(bmp.get(i));//
        }

        adapter = new MyPageAdapter(listViews);// 构造adapter
        pager.setAdapter(adapter);// 设置适配器
        Intent intent = getIntent();
        int id = intent.getIntExtra("ID", 0);
        pager.setCurrentItem(id);
    }

    private void initListViews(Bitmap bm) {
        if (listViews == null)
            listViews = new ArrayList<View>();
        ImageView img = new ImageView(this);// 构造textView对象
        img.setBackgroundColor(0xff000000);
        img.setImageBitmap(bm);
        img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        listViews.add(img);// 添加view
    }

    private ViewPager.OnPageChangeListener  pageChangeListener =  new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
            // 滑动中。。
        }

        @Override
        public void onPageSelected(int i) {
            // 页面选择响应函数
            count = i;
        }

        @Override
        public void onPageScrollStateChanged(int i) {
            // 滑动状态改变
        }
    };

    private class MyPageAdapter extends PagerAdapter{

        private ArrayList<View>   listViews;
        private int size; // 页数

        public MyPageAdapter(ArrayList<View> listViews) {
             // 构造函数
            // 初始化viewpager的时候给的一个页面
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        public void setListViews(ArrayList<View> listViews) {
            // 自己写的一个方法用来添加数据
            this.listViews = listViews;
            size = listViews == null ? 0 : listViews.size();
        }

        @Override
        public int getCount() {
            if(listViews !=null){
                return  listViews.size();
            }
            return 0;  // 返回数量
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 返回view对象
                container.addView(listViews.get(position % size), 0);

            return listViews.get(position % size);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(listViews.get(position % size));
        }
    }
}
